package com.lemon.smartsafetynew.ui.activity.record;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.lemon.smartsafety.R;
import com.lemon.smartsafety.app.AppManager;
import com.lemon.smartsafety.listener.RecyclerTClickListener;
import com.lemon.smartsafetynew.entity.FileInfo;
import com.lemon.smartsafetynew.entity.common.DateTypeEnum;
import com.lemon.smartsafetynew.entity.record.CertificateVo;
import com.lemon.smartsafetynew.ui.activity.base.SwipeBackActivity;
import com.lemon.smartsafetynew.ui.adapter.record.FilesAdapter;
import com.lemon.smartsafetynew.util.common.CommonUtil;
import com.lemon.smartsafetynew.util.common.UrlFormatUtils;
import com.lemon.smartsafetynew.util.constants.KeyConstants;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 一企一档特殊资格证详情
 * Created by JiangAn on 2018/5/28.
 */

public class RecordCertificateDetailActivity extends SwipeBackActivity implements RecyclerTClickListener<FileInfo> {

    @Bind(R.id.iv_return)
    ImageView ivReturn;
    @Bind(R.id.tv_trainer_name)
    TextView tvTrainerName;
    @Bind(R.id.tv_birth_date)
    TextView tvBirthDate;
    @Bind(R.id.tv_certificate_type)
    TextView tvCertificateType;
    @Bind(R.id.tv_send_certificate)
    TextView tvSendCertificate;
    @Bind(R.id.tv_valid_date)
    TextView tvValidDate;
    @Bind(R.id.tv_next_check_date)
    TextView tvNextCheckDate;
    @Bind(R.id.tv_space_remark)
    TextView tvSpaceRemark;
    @Bind(R.id.rySafeFile)
    RecyclerView rySafeFile;
    @Bind(R.id.tv_file_none)
    TextView tvFileNone;

    private Boolean isShowReturn = false;
    private static CertificateVo data;

    @Override
    protected void initTitle() {
        this.setBarTitle(getString(R.string.record_special_task_info));
    }

    @Override
    protected void getBundleArgs() {
        super.getBundleArgs();

        Bundle bundle = getIntent().getExtras();
        if (bundle != null) {
            data = (CertificateVo) bundle.getSerializable(KeyConstants.KEY_SPECIAL_OPERATOR_VO);
            isShowReturn = bundle.getBoolean("isShowReturn");
        }
    }

    @Override
    protected void callFinish() {
        AppManager.getAppManager().finishActivity(RecordCertificateDetailActivity.this);
    }

    @Override
    protected int getContentViewId() {
        return R.layout.activity_record_certificate_detail;
    }

    @Override
    protected void initViews() {
        super.initViews();
        if (isShowReturn) {
            ivReturn.setVisibility(View.VISIBLE);
        } else {
            ivReturn.setVisibility(View.INVISIBLE);
        }
        if (data != null) {
            tvTrainerName.setText(CommonUtil.avoidNullString(data.getName()));
            tvBirthDate.setText(CommonUtil.secondToTimeStr(data.getBirth_date(), DateTypeEnum.DAY));
            tvCertificateType.setText(CommonUtil.avoidNullString(data.getLicence_type()));
            tvSendCertificate.setText(CommonUtil.avoidNullString(data.getIssue_department()));
            tvValidDate.setText(CommonUtil.secondToTimeStr(data.getEnd_date(), DateTypeEnum.DAY));
            tvNextCheckDate.setText(CommonUtil.secondToTimeStr(data.getNext_date(), DateTypeEnum.DAY));
            tvSpaceRemark.setText(CommonUtil.avoidNullString(data.getRemarks()));
            initAdapter();
        }
    }

    private void initAdapter() {
        if (data.getFiles() != null && data.getFiles().size() > 0) {
            tvFileNone.setVisibility(View.GONE);
            rySafeFile.setVisibility(View.VISIBLE);
            int num = data.getFiles().size();
            List<FileInfo> fileList = new ArrayList<>();
            for (int i = 0; i < num; i++) {
                FileInfo fileinfo = new FileInfo();
                fileinfo.setName(data.getFiles().get(i).getLicence_name());
                fileinfo.setUrl(UrlFormatUtils.formatFiles(data.getFiles().get(i).getLicence_url()));
                fileList.add(fileinfo);
            }
            LinearLayoutManager linearLayoutManager = new LinearLayoutManager(RecordCertificateDetailActivity.this);
            linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
            rySafeFile.setLayoutManager(linearLayoutManager);
            rySafeFile.setNestedScrollingEnabled(false);
            FilesAdapter mAdapter = new FilesAdapter(fileList, this);
            rySafeFile.setAdapter(mAdapter);
        }else{
            tvFileNone.setVisibility(View.VISIBLE);
            rySafeFile.setVisibility(View.GONE);
        }
    }

    @Override
    protected void initListener() {
        super.initListener();
        ivReturn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AppManager.getAppManager().finishActivity(RecordCertificateDetailActivity.this);
                AppManager.getAppManager().finishActivity(RecordCertificateActivity.class);
            }
        });
    }

    @Override
    public void onItemClick(FileInfo data, int position) {
        downloadAndShowFile(data.getUrl(), data.getName(), this, "特种作业操作证人员");
    }

}




